import {_decorator, Component, Sprite} from 'cc';
import {pb} from "db://assets/script/network/ws/proto";

const {ccclass, property} = _decorator;

@ccclass('CardView')
export class CardView extends Component {
    @property({
        type: Sprite,
        tooltip: "麻将牌 icon"
    })
    cardString: Sprite
    tileIndex: number // 牌 id
    tid: pb.TileId

    // 更新牌的 icon
    updateCardIconByTid(tid: pb.TileId, tileIndex: number): boolean {
        const frames = this.cardString.spriteAtlas.getSpriteFrames();
        for (const f of frames) {
            if (f.name === "string_" + tid) {
                // 就是这张
                this.cardString.spriteFrame = f
                this.tileIndex = tileIndex
                this.tid = tid
                return true
            }
        }
        return false
    }

    onLoad() {
        // if (!this.tid) {
        //     // 不能移动
        // }
        // this.node.on(Node.EventType.TOUCH_START, (event: EventTouch) => {
        //     if (!this.originPos) {
        //         // 设置初始位置
        //         this.originPos = this.node.getPosition()
        //         console.log("set origin position", this.tid, this.originPos)
        //     }
        //     this.selected()
        // })
        //
        // this.node.on(Node.EventType.TOUCH_CANCEL, () => {
        //     console.log("touch cancel")
        // })
        // this.node.on(Node.EventType.TOUCH_END, (event: EventTouch) => {
        //     console.log("touch end")
        //     // this.moveBack()
        //     const newPos = ViewUtil.calculateScreenPosToSpacePos(event, this.node.parent)
        //     if (newPos.y > this.originPos.y + 80) {
        //         // 超过200 px，默认已经出牌了
        //         // this.node.destroy()
        //         this.node.active = false
        //     } else {
        //         // 还原选中的状态
        //         this.selected()
        //     }
        // })
        //
        // this.node.on(Node.EventType.TOUCH_MOVE, (event: EventTouch) => {
        //     const newPos = ViewUtil.calculateScreenPosToSpacePos(event, this.node.parent)
        //     if (newPos.y < 0) {
        //         // 不能移到桌子以下
        //         return
        //     }
        //     console.log("move to", this.node.parent.getPosition(), newPos)
        //     this.node.setPosition(newPos)
        // })
    }
}



